/*---------------------------------------------------------------------------
 *-Version :0.1 @ 08/20/2008  modifed@08/30/2008                            *
 *-Auther : Chen Ju                                                         *
 *-Auther Email: sammy105@gmail.com                                         *
 *-Auther site : http://www.kylogs.com/blog                                 * 
 *-This file defines all stack releated functions                           *
 *-related funcitons.                                                       * 
 *---------------------------------------------------------------------------
 */
#include "easyc_ds.h"
#include <stdlib.h>
#include<stddef.h>
#include<stdio.h>
/*
* pop an element from stack
*/
eType easyc_pop(c_stack *s){
    if(s->point<=0) {
        printf("EmptyStackException\n");
        return 0;
    }
    int temp=s->point;
    s->point--;
    return *(s->base+temp);       
}

/* 
*  push an element to stack 
*  If fail return 0;else return a positive integer.
*/
int easyc_push(eType e, c_stack *s){
    int i=0;	
    if(s->point==s->size){
        s->base=realloc(s->base,sizeof(eType)*(s->size+s->base_size));
    }
    s->point++;
    *(s->base+s->point)=e;
    return 1;
}

/*get top element*/
eType easyc_get_top(c_stack *s){
    return *(s->base+s->point);
}

/* creat stack with default size */
c_stack make_stack(int size){
    c_stack s;
    s.size=size;
    s.base_size=10;
    s.point=0;
    s.base=(eType *)malloc(sizeof(eType)*size);
    s.pop=easyc_pop;
    s.push=easyc_push;
    s.get_top=easyc_get_top;
    return s;
}

/* remove the stack, and release the memory space */
void easyc_del(c_stack *s){
    while(s->size>=0){
        eType *t=s->base+s->size;
        free(t);
        s->size--;
    }
    free(s);
}


